<?php

namespace App\Http\Controllers;

use App\Console\Commands\migrationAppoints;
use App\Exceptions\APIErrorException;
use App\Exceptions\StringHelper;
use App\Jobs\AppointList;
use App\Models\Appoint;
use App\Models\Family;
use App\Models\HospitalAppointType;
use App\Models\Interview;
use App\Models\old\ChildInfo;
use App\Models\old\OldHospital;
use App\Models\old\UserLogin;
use App\Models\old\UserParent;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use PHPUnit\Util\Exception;

class AppointController extends Controller
{
    public function queue(Request $request){
        if(!$request->input('id')){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id参数不正确'],500);
        }
        $id = $request->input('id');
        $appoint = Appoint::query()->where('id',$id)->first();
        if(!$appoint){
            return new JsonResponse(['code'=>500,'msg'=>'失败，id不存在'],500);
        }
        try {
            //AppointList::dispatch($appoint);
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }catch (\Exception $exception){
            return new JsonResponse(['code'=>500,'msg'=>$exception->getMessage()],500);
        }
    }

    public function appointView(Request $request)
    {
        $user = Auth::guard('hospital-web')->user();
        $hospitalid = $user->hospitalid;
        $userDoctor = OldHospital::query()->where('hospitalid',$hospitalid)->first();
        $id = $request->input('id');
        $appoint = \App\Models\old\Appoint::query()
            ->where('id',$id)
            ->where('doctorid',$userDoctor->userid)
            ->first();
        if($appoint){
            $childInfo = ChildInfo::query()
                ->where('id',$appoint->childid)
                ->first();
            $userParent = UserParent::query()->where('userid',$childInfo->userid)->first();
            $data = [
                'project'=>\App\Models\old\Appoint::$typeText[$appoint->type],
                'date'=>date('Y-m-d',$appoint->appoint_date),
                'time'=>\App\Models\old\Appoint::$timeText[$appoint->appoint_time],
                'person'=>$appoint->name(),
                'statusTxt'=>\App\Models\old\Appoint::$stateText[$appoint->state],
                'status'=>$appoint->state,
                'phone'=>$appoint->phone,
                'mother'=>$userParent->mother,
                'birthday'=> date('Y年m月d日',$childInfo->birthday)
            ];
            return new JsonResponse(['code'=>200,'msg'=>'成功','data'=>$data],200);
        }else{
            $appoint = Appoint::query()
                ->where('id',$id)
                ->where('hospital_id',$userDoctor->userid)
                ->first();
            if($appoint){
                $family = Family::query()->where('id',$appoint->family_id)->first();
                $data = [
                    'project'=>$appoint->appointType->name,
                    'date'=>$appoint->appoint_date,
                    'time'=>$appoint->interval->start."-".$appoint->interval->end,
                    'person'=>$appoint->family->name,
                    'statusTxt'=>Appoint::$stateTxt[$appoint->status],
                    'status'=>$appoint->status,
                    'phone'=>$appoint->user->phone,
                    'mother'=>$family->childOne->mother,
                    'birthday'=> $family->birthday
                ];
                return new JsonResponse(['code'=>200,'msg'=>'成功','data'=>$data],200);
            }else{
            return new JsonResponse(['code'=>500,'msg'=>'预约记录不存在'],500);
            }
        }
    }

    public function appointDone(Request $request)
    {
        $user = Auth::guard('hospital-web')->user();
        $hospitalid = $user->hospitalid;
        $userDoctor = OldHospital::query()->where('hospitalid',$hospitalid)->first();
        $id = $request->input('id');
        $appoint = \App\Models\old\Appoint::query()
            ->where('id',$id)
            ->whereIn('state',[1,5])
            ->where('doctorid',$userDoctor->userid)
            ->first();
        if($appoint){
            $appoint->state = 2;
            $appoint->save();
            return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
        }else{
            $appoint = Appoint::query()
                ->where('id',$id)
                ->whereIn('status',[1,5])
                ->where('hospital_id',$userDoctor->userid)
                ->first();
            if($appoint){
                $appoint->status = 2;
                $appoint->save();
                return new JsonResponse(['code'=>200,'msg'=>'成功'],200);
            }
            return new JsonResponse(['code'=>500,'msg'=>'预约已取消或已完成'],500);
        }
    }
    public function appointList(Request $request){
        $user = Auth::guard('hospital-web')->user();
        $hospitalid = $user->hospitalid;

        $userDoctor = OldHospital::query()->where('hospitalid',$hospitalid)->first();
        if(in_array($userDoctor->userid,[184793,176156])){
            $query = Appoint::query()
                ->where('eb_appoint.hospital_id', $userDoctor->userid);
            $appoint_date = $request->input('appoint_date');
            if ($appoint_date) {
                $query->where('eb_appoint.appoint_date', $appoint_date);
            }
            $type = $request->input('type');
            if ($type) {
                $type = migrationAppoints::$typeText[$type];
                $query->where('eb_appoint.type', $type);
            }
            $state = $request->input('state');
            if ($state) {
                $query->where('eb_appoint.status', $state);
            }
            $phone = $request->input('phone');
            if ($phone) {
                $query->join('eb_family', 'eb_family.id', "=", 'eb_appoint.family_id');
                $query->where('eb_family.phone', $phone);
            }
            $sort = $request->input('sort');
            $sort_type = $request->input('sort_type');
            if ($sort) {
                if ($sort == 'birthday') {
                    $sort_type = $sort_type ? $sort_type : 'desc';
                    $query->join('eb_family', 'eb_family.id', "=", 'eb_appoint.family_id');
                    $query->orderBy('eb_family.birthday', $sort_type);
                }
            } else {
                $query->orderBy('eb_appoint.id', 'desc');
            }

            $appoints = $query->paginate();
            foreach ($appoints as $v) {
                if($v->family_id) {
                    $childInfo = Family::query()
                        ->where('id', $v->family_id)
                        ->first();
                    $v->name = $childInfo ? $childInfo->name : "";
                    $v->appoint_date = date('Y年m月d日', strtotime($v->appoint_date));
                    $v->type = HospitalAppointType::query()->where('id', $v->type)->value('name');
                    $v->state = Appoint::$stateTxt[$v->status];
                    $v->birthday = $childInfo?date('Y年m月d日', strtotime($childInfo->birthday)):"";

                }else{
                    $appoint = \App\Models\old\Appoint::query()->where('id',$v->id)->first();
                    $v->name = $appoint->name();
                    $v->appoint_date = date('Y年m月d日', $appoint->appoint_date);
                    $v->type = \App\Models\old\Appoint::$typeText[$appoint->type];
                    $v->state = \App\Models\old\Appoint::$stateText[$appoint->state];
                    $childInfo = ChildInfo::query()
                        ->where('id', $appoint->childid)
                        ->first();
                    $v->birthday = date('Y年m月d日', $childInfo->birthday);
                }

            }
        }else {
            $query = \App\Models\old\Appoint::query()
                ->where('appoint.doctorid', $userDoctor->userid);
            $appoint_date = $request->input('appoint_date');
            if ($appoint_date) {
                $query->where('appoint_date', strtotime($appoint_date));
            }
            $type = $request->input('type');
            if ($type) {
                $query->where('appoint.type', $type);
            }
            $state = $request->input('state');
            if ($state) {
                $query->where('appoint.state', $state);
            }
            $phone = $request->input('phone');
            if ($phone) {
                $query->where('appoint.phone', $phone);
            }
            $sort = $request->input('sort');
            $sort_type = $request->input('sort_type');
            if ($sort) {
                if ($sort == 'birthday') {
                    $sort_type = $sort_type ? $sort_type : 'desc';
                    $query->join('child_info', 'child_info.id', "=", 'appoint.childid');
                    $query->orderBy('child_info.birthday', $sort_type);
                }
            } else {
                $query->orderBy('id', 'desc');
            }

            $appoints = $query->paginate();
            foreach ($appoints as $v) {
                $v->name = $v->name();
                $v->appoint_date = date('Y年m月d日', $v->appoint_date);
                $v->type = \App\Models\old\Appoint::$typeText[$v->type];
                $v->state = \App\Models\old\Appoint::$stateText[$v->state];
                $childInfo = ChildInfo::query()
                    ->where('id', $v->childid)
                    ->first();
                $v->birthday = date('Y年m月d日', $childInfo->birthday);

            }
        }
        return new JsonResponse(['code'=>200,'msg'=>'成功','data'=>$appoints],200);
    }

    public function childs(Request $request)
    {
        $user = Auth::guard('hospital-web')->user();
        $hospitalid = $user->hospitalid;
        $userDoctor = OldHospital::query()->where('hospitalid',$hospitalid)->first();
        $query = ChildInfo::query()
            ->join('doctor_parent','child_info.userid','=','doctor_parent.parentid')
            ->where('doctor_parent.doctorid',$userDoctor->userid)
            ->orderBy('doctor_parent.createtime','desc');
        $birthday = $request->input('birthday');
        if($birthday){
            $query->where('child_info.birthday',strtotime($birthday));
        }
        $childs = $query->paginate();

        foreach($childs as $v){
            $userLogin = UserLogin::query()
                ->where('userid',$v->userid)
                ->where('phone','!=',0)
                ->first();
            if($userLogin){
                $v->phone = $userLogin->phone;
            }
            $v->birthday = date('Y-m-d',$v->birthday);
        }
        return new JsonResponse(['code'=>200,'msg'=>'成功','data'=>$childs],200);
    }
}
